// pages/motion/motionAudio/motionAudio.js
var utils = require('../../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        videoimage: "block", //默认显示封面
        bindplay: null,
        _index: 0,
        isStart: false,
        curBdIndex: 0, //点击收藏状态
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.videoList();
    },

    //列表显示
    videoList: function () {
        var that = this;
        utils.showLoading();
        utils.http(httpUrl + "/mobile/article/video_article_list", { }, function (res) {
           // console.log(res);
            utils.hideLoading();
            if (res.state == 1) {
                var dataList = [];
                for (var i in res.datas) {
                    var time = Date.parse(new Date(res.datas[i].create_time));
                    var timeData1 = utils.getPastTime(time);
                    var port = {
                        id: res.datas[i].id,
                        title: res.datas[i].title,
                        introduction: res.datas[i].introduction,
                        photo: res.datas[i].photo,
                        reading: res.datas[i].reading,
                        col_num: res.datas[i].col_num,
                        my_is_col: res.datas[i].my_is_col,
                        my_is_zan: res.datas[i].my_is_zan,
                        content: res.datas[i].content,
                        create_time: timeData1

                    }
                    dataList.push(port);
                }
                console.log(dataList);
                that.setData({
                    videolist: dataList
                });
            } else {
                utils.message(res.datas);
            }
        })

    },
    //点击播放按钮，封面图片隐藏,播放视频
    bindplay: function (e) {
        var that = this;
        var index = e.currentTarget.dataset.id;
        var itemid = e.currentTarget.dataset.itemid;
        //停止正在播放的视频
        var videoContextPrev = wx.createVideoContext("myVideo" + that.data._index)
        videoContextPrev.seek(0)
        videoContextPrev.pause();
        // setTimeout(function () {
        //     //将点击视频进行播放
        //     var videoContext = wx.createVideoContext('myVideo' + index)
        //     videoContext.play();
        //     that.data._index = index;
        // }, 100)
        utils.showLoading()
        utils.http(httpUrl + "/mobile/article/video_article_detail", {
            id: itemid
        }, function (res) {
            utils.hideLoading();
            if (res.state == 1) {
                that.setData({
                    videoDetail: res.datas
                })
                that.setData({
                    id: index
                })
                var videoContext = wx.createVideoContext('myVideo' + index)
                videoContext.play();
                that.data._index = index;

            } else {
                wx.showModal({
                    title: '提示',
                    content: res.datas,
                    success(res) {
                        if (res.confirm) {
                            utils.http(httpUrl + "/mobile/article/add_order", {
                                id: itemid
                            }, function (res) {
                                wx.requestPayment({
                                    timeStamp: res.timeStamp,
                                    nonceStr: res.nonceStr,
                                    package: res.package,
                                    signType: res.signType,
                                    paySign: res.paySign,
                                    success(res) {
                                        wx.showToast({
                                            title: "支付成功",
                                            icon: 'success',
                                            duration: 2000
                                        })
                                        that.setData({
                                            id: index
                                        })
                                        var videoContext = wx.createVideoContext('myVideo' + index)
                                        videoContext.play();
                                        that.data._index = index
                                    },
                                    fail(res) {
                                        that.setData({
                                            id: -0
                                        })
                                    }
                                })
                            })

                        } else if (res.cancel) {
                            that.setData({
                                id: -0
                            })
                        }
                    }
                })

            }

        })

    },


     //视频收藏
    oNstrartTap: function (e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        for (var i in that.data.videolist) {
            if (that.data.videolist[i].id == id) {
                utils.http(httpUrl + "/mobile/common/make_col", {
                    be_id: id,
                    type: 3
                }, function (res) {
                    if (res.state == 1) {
                        utils.message(res.datas);
                        that.videoList();
                    } else {
                        utils.message(res.datas);
                    }
                })
            } else {
                // console.log("111111111111111111111");
            }
        }

    },
    //视频点赞
    oNzanTap: function (e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        for (var i in that.data.videolist) {
            if (that.data.videolist[i].id == id) {
                utils.http(httpUrl + "/mobile/common/make_zan", {
                    be_id: id,
                    type: 3
                }, function (res) {
                    if (res.state == 1) {
                        utils.message(res.datas);
                        that.videoList();
                    } else {
                        utils.message(res.datas);
                    }
                })
            } else {
                // console.log("111111111111111111111");
            }
        }

    },
})